<template>
  <el-dialog
    :title="!dataForm.id ? $t('brand.add') : $t('user.editBtn')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="5vh"
    width="780px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      :label-width="this.$i18n.t('language') === 'language' ? '150px' : '80px'"
    >
      <el-form-item :label="$t('shop.cyclopediaName')" prop="name">
        <el-input size="small" maxlength="20" v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item :label="$t('shop.cyclopediaTitle')" prop="title">
        <el-input size="small" maxlength="36" v-model="dataForm.title"></el-input>
      </el-form-item>
      <el-form-item :label="$t('platform.cyclopediaImg')" prop="img">
        <img-upload v-model="dataForm.img"></img-upload>
        <span>{{$t('platform.recommImgSize')}}750*400</span>
      </el-form-item>
      <el-form-item :label="$t('publics.status')" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="'1'">{{ $t("publics.publicar") }}</el-radio>
          <el-radio :label="'0'">{{ $t("publics.cancel") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('shop.noticeContent')" class="notice-content">
        <tiny-mce ref="tinyMce" v-model="dataForm.role" v-if="visible" :tinymceHeight="250"></tiny-mce>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <div class="default-btn" @click="visible = false">{{
          $t("remindPop.cancel")
        }}</div>
      <div class="default-btn primary-btn" @click="dataFormSubmit()">{{
          $t("remindPop.confirm")
        }}</div>
    </span>
  </el-dialog>
</template>

<script>
import TinyMce from '@/components/tiny-mce'
import {isHtmlNull} from '@/utils/index.js'
import ImgUpload from '@/components/img-upload'
import SelectAccountRange from '@/components/SelectAccountRange'
export default {
  data () {
    var validateTitle = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$i18n.t('shop.titCanNoBlank')))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      dataForm: {
        title: null,
        name: null,
        img: null,
        status: '1',
        role: '' // 发送时间
      },
      isSubmit: false,
      dataRule: {
        title: [
          { required: true, message: this.$i18n.t('shop.titCanNoBlank'), trigger: 'blur' },
          {validator: validateTitle, trigger: 'blur'}
        ],
        img: [
          { required: true, message: this.$i18n.t('platform.cyclopediaImgNoNull'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$i18n.t('shop.cyclopediaNameNoBlank'), trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    TinyMce,
    ImgUpload,
    SelectAccountRange
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isSubmit = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          console.log(this.dataForm.id)

          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/admin/cyclopedia/info/' + this.dataForm.id),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              this.dataForm = data
              this.$nextTick(() => this.$refs.tinyMce.setContent(this.dataForm.role))
            })
          }else{
            this.dataForm.role = ""
            this.$nextTick(() => this.$refs.tinyMce.setContent(null))
          }
        })

    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.isSubmit) {
            return false
          }
          if (isHtmlNull(this.dataForm.role)) {
            this.$message.error(this.$i18n.t('shopProcess.cyclopediaTip'))
            return
          }
          const obj = JSON.parse(JSON.stringify(this.dataForm))
          this.isSubmit = true
          this.$http({
            url: this.$http.adornUrl('/admin/cyclopedia'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(obj)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('remindPop.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
                this.dataForm.role = ''
              }
            })
          }).catch((e) => {
            this.isSubmit = false
          })
        }
      })
    }
  }
}
</script>

